from PyQt4 import QtCore, QtGui
 
from limber.ui.dialogs.ui_addability import Ui_AddAbilityDialog
from limber.ui.dialogs import utils


class AddAbilityDialog(QtGui.QDialog, Ui_AddAbilityDialog):
    
    show_ability_details_request = QtCore.pyqtSignal(
                                   name='showAbilityDetailsRequest')
    
    def __init__(self, parent=None, abilities=()):
        super(AddAbilityDialog, self).__init__(parent)
        
        self.setupUi(self)
        
        self.ability_combo.addItems(abilities)
        self.ability_details_button.clicked.connect(
            self.show_ability_details_request.emit)
    
    def current_ability(self):
        '''Return the currently selected ability or None.'''
        
        text = self.ability_combo.currentText()
        if not text:
            return None
        return unicode(text)
    
    @staticmethod
    def get_ability(parent=None, abilities=()):
        '''Pop-ups a modal dialog with *parent* and *abilities* and
        return the ability choosen or `None`.
        '''
        
        dlg = AddAbilityDialog(parent, abilities)
        if dlg.exec_():
            text = dlg.ability_combo.currentText()
            if text:
                return unicode(text)
        
        return None

utils.register_dialog(AddAbilityDialog, 'add-ability-dialog')